// 创建birds.js
var express = require('express')
var getApi = express.Router()// 创建一个路由实例

const data = [
  {
    "id": 590,
    "name": "ImageKnife",
    "briefDesc": "Axios是一个基于 promise 的网络请求库，可以运行 node.js 和浏览器中。本库基于Axios原库进行适配，使其可以运行在 OpenHarmony，并沿用其现有用法和特性。",
    "author": "opensource",
    "licence": "Apache 2.0 License",
    "gitUrl": "https://gitee.com/openharmony-tpc/Axios",
    "keyword": "OpenHarmony,http,request,promise,axios",
  },
  {
    "id": 591,
    "name": "zxing",
    "briefDesc": "zxing是一个解析/生成二维码的库，该项目是ts版本的zxing，在js-zxing的基础上补齐了与java-zxing库的差异的编码解码能力。",
    "author": "opensource",
    "licence": "MIT License",
    "gitUrl": "https://gitee.com/openharmony-tpc/zxing",
    "keyword": "zxing,OpenHarmony",
  }
]

const userInfo = {
  name: '张三',
  age: 25,
  sex: "男",
  province: "浙江",
  city: "杭州",
  country: "中国",
  area: "滨江",
  score: 96,
  id: 997,
  phone: "13714389438",
  pwd: "123456"
}

// 中间件功能
getApi.use(function timeLog(req, res, next) {
  console.log('Time: ', new Date().toLocaleString())
  next()
})
// 访问/getApi
getApi.get('/', function (req, res) {
  res.send('访问了getApi,请求成功，开始返回数据')
})

// 访问/getList
getApi.get('/getList', function (req, res) {
  res.send(data)
})
// 访问/getDetail/:id
getApi.get('/getDetail/:id', function (req, res) {
  // 获取参数
  let requestId = req.params.id.toString();
  let count = 0;
  data.forEach(function (item, index, self) {
    if (item && item.id && requestId === item.id.toString()) {
      count++;
      res.send(item)
      return
    }
  });

  if (count === 0) {
    res.send('cpiuld not find data which id is ${requestId}')
  }
})

// 访问/query
getApi.get('/query', function (req, res) {
  let params = req.query
  console.log('query  params :----> ', JSON.stringify(params))
  if (params && params.phone && params.pwd && params.phone === "13714389438" && params.pwd === "123456") {
    res.send(userInfo)
  } else {
    res.send({
      status: 404,
      msg: 'params wrong',
      data: 'could not found userinfo with params:' + JSON.stringify(params)
    })
  }

})


// 访问/getInfo/:id
getApi.get('/getInfo/:city/:id/query', function (req, res) {
  // 获取参数
  let requestId = req.params.id.toString();
  let city = req.params.city.toString();
  if (requestId === '634' && city === 'hangzhou') {
    res.send('city is :---> ' + city)
  } else {
    res.send("参数错误，你丫的再检查一次")
  }

})

// 访问/getPlus/:id
getApi.get('/getPlus/query/:city/:id', function (req, res) {
  // 获取参数
  let requestId = req.params.id.toString();
  let city = req.params.city.toString();
  let params = req.query
  console.log('query  params :----> ', params.phone)
  if (requestId === '634' && city === 'hangzhou' && params && params.phone && params.phone === "13714389438") {
    res.send('city is :---> ' + city)
  } else {
    res.send("参数错误，你丫的再检查一次")
  }

})

// 访问/getPlus/:id
getApi.get('/getTest', function (req, res) {
  // 获取参数
  let headers = req.headers ? JSON.stringify(req.headers) : '';
  let params = req.params ? JSON.stringify(req.params) : '';
  let query = req.query ? JSON.stringify(req.query) : '';
  console.log('params :----> ', params)
  console.log('query   :----> ', query)
  console.log('headers   :----> ', headers)
  res.json({
    header: headers,
    params: params,
    query: query,
    method: req.method,
    url: req.originalUrl,
    msg: '服务器回复'
  })

})
// 访问/getPlus/:id
getApi.get('/getTest1', function (req, res) {
  // 获取参数
  let headers = req.headers ? JSON.stringify(req.headers) : '';
  let params = req.params ? JSON.stringify(req.params) : '';
  let query = req.query ? JSON.stringify(req.query) : '';
  console.log('params :----> ', params)
  console.log('query   :----> ', query)
  console.log('headers   :----> ', headers)
  let buf = Buffer.from('test callback')
  // let buf = stringToArrayBuffer('test callback')
  console.log('getTest buf  :----> '+ (buf instanceof Uint8Array))
  console.log('getTest buf content :----> '+ buf)
  res.write(buf)
  res.end()
})


module.exports = getApi